package com.dotools.utils;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

public class RomUtils {
    public final static String CyModName = "cyanogenmod";

    public static boolean isHuaWeiRom() {
        if (Build.MANUFACTURER.equals("HUAWEI"))
            return true;
        else
            return false;
    }

    public static void openAllPhonePermission(Context context, String MANUFACTURER, int requestCode) {
        if (MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
            DevicesUtils_MIUI.openMiuiPermissionsActivity(context, requestCode);
        } else if (MANUFACTURER.equalsIgnoreCase("HUAWEI")) {
            openHuaweiPermission(context, requestCode);
        } else if (MANUFACTURER.equalsIgnoreCase("Meizu")) {
            openMeiZuPermission(context, requestCode);
        } else if (MANUFACTURER.equalsIgnoreCase("Sony")) {
            openSonyPermission(context, requestCode);
        } else if (MANUFACTURER.equalsIgnoreCase("OPPO")) {
            openOPPOPermission(context, requestCode);
        } else {
            startDetailSetting(context, requestCode);
        }

    }


    public static void openAllPhoneShortcut(Context context, String MANUFACTURER, int requestCode) {
        if (MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
            DevicesUtils_MIUI.openMiuiPermissionsActivity(context, requestCode);
        } else if (MANUFACTURER.equalsIgnoreCase("HUAWEI")) {
            openHuaweiPermission(context, requestCode);
        }
//        else if (MANUFACTURER.equalsIgnoreCase("Meizu")) {
//            openMeiZuPermission(context, requestCode);
//        }
        else if (MANUFACTURER.equalsIgnoreCase("Sony")) {
            openSonyPermission(context, requestCode);
        } else if (MANUFACTURER.equalsIgnoreCase("OPPO")) {
            openOPPOShortcut(context, requestCode);
        } else if (MANUFACTURER.equalsIgnoreCase("VIVO")) {
            openVIVOShortcut(context, requestCode);
        } else {
            startDetailSetting(context, requestCode);
        }
    }


    /**
     * 打开华为自启动
     */
    public static void openHuaweiPermissionActivity(Context context, int requestCode) {
        if (!isHuaWeiRom())
            return;
        Intent intent = new Intent("huawei.intent.action.HSM_BOOTAPP_MANAGER");
        if (Build.VERSION.SDK_INT >= 23) {
            intent.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");
        } else {
            intent.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.bootstart.BootStartActivity");
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("extra_pkgname", context.getPackageName());
        if (context instanceof Activity) {
            Activity a = (Activity) context;
            a.startActivityForResult(intent, requestCode);
        } else {
            context.startActivity(intent);
        }
    }

    /**
     * 打开华为受保护的应用
     */
    public static void openHuaweiProtectActivity(Context context) {
        if (!isHuaWeiRom())
            return;
        Intent localIntent13 = new Intent();
        localIntent13.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");
        localIntent13.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(localIntent13);
    }

    /**
     * 悬浮窗
     *
     * @param context
     * @param requestCode
     */
    public static void openHuaweiFloatWindowActivity(Context context, int requestCode) {
        if (!isHuaWeiRom())
            return;
        Intent intent = new Intent("huawei.intent.action.NOTIFICATIONMANAGER");
        if (Build.VERSION.SDK_INT >= 23) {
            intent.setClassName("com.huawei.systemmanager",
                    "com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity");
        } else {
            intent.setClassName("com.huawei.systemmanager",
                    "com.huawei.notificationmanager.ui.NotificationManagmentActivity");
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("extra_pkgname", context.getPackageName());
        if (isIntentAvailable(context, intent)) {
            if (context instanceof Activity) {
                Activity a = (Activity) context;
                a.startActivityForResult(intent, requestCode);
            } else {
                context.startActivity(intent);
            }
        }
    }

    /**
     * 华为的权限管理页面 传activity会有返回code  其他context则没有
     */
    public static void openHuaweiPermission(Context context, int requestCode) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClassName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");
        if (isIntentAvailable(context, intent)) {
            if (context instanceof Activity) {
                Activity a = (Activity) context;
                a.startActivityForResult(intent, requestCode);
            } else {
                context.startActivity(intent);
            }
        }
    }



    /**
     * 判断是否有可以接受的Activity
     *
     * @param context
     * @return
     */
    public static boolean isIntentAvailable(Context context, Intent intent) {
        if (intent == null)
            return false;
        return context.getPackageManager().queryIntentActivities(intent, PackageManager.GET_ACTIVITIES).size() > 0;
    }

    /**
     * /打开魅族应用详情页 传activity会有返回code  其他context则没有
     */
    public static void openMeiZuPermission(Context context, int requestCode) {
        Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.putExtra("packageName", context.getPackageName());
        Activity a = null;
        try {
            if (context instanceof Activity) {
                a= (Activity) context;
                a.startActivityForResult(intent, requestCode);
                a.finish();
            } else {
                context.startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
            startDetailSetting(context, requestCode);
        }
    }


    /**
     * 索尼
     *
     * @param context
     * @param requestCode
     */
    public static void openSonyPermission(Context context, int requestCode) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("packageName", context.getPackageName());
        ComponentName comp = new ComponentName("com.sonymobile.cta", "com.sonymobile.cta.SomcCTAMainActivity");
        intent.setComponent(comp);
        if (context instanceof Activity) {
            Activity a = (Activity) context;
            a.startActivityForResult(intent, requestCode);
        } else {
            context.startActivity(intent);
        }
    }


    /**
     * @param context
     * @param requestCode
     */
    public static void openOPPOPermission(Context context, int requestCode) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("packageName", context.getPackageName());
        ComponentName comp = new ComponentName("com.color.safecenter", "com.color.safecenter.permission.PermissionManagerActivity");
        intent.setComponent(comp);
        if (context instanceof Activity) {
            Activity a = (Activity) context;
            a.startActivityForResult(intent, requestCode);
        } else {
            context.startActivity(intent);
        }
    }


    /**
     * @param context
     * @param requestCode
     */
    public static void openOPPOShortcut(Context context, int requestCode) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = ComponentName.unflattenFromString("com.oppo.launcher/.shortcut.ShortcutSettingsActivity");
            intent.setComponent(comp);
            if (context instanceof Activity) {
                Activity a = (Activity) context;
                a.startActivityForResult(intent, requestCode);
            } else {
                context.startActivity(intent);
            }
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            startDetailSetting(context, requestCode);
        }
    }

    /**
     * @param context
     * @param requestCode
     */
    public static void openVIVOShortcut(Context context, int requestCode) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = ComponentName.unflattenFromString("com.bbk.launcher2/.installshortcut.PurviewActivity");
            intent.setComponent(comp);
            if (context instanceof Activity) {
                Activity a = (Activity) context;
                a.startActivityForResult(intent, requestCode);
            } else {
                context.startActivity(intent);
            }
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

    //打开该应用详情 传activity会有返回code  其他context则没有
    public static void startDetailSetting(Context context, int requestCode) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        if (context instanceof Activity) {
            Activity a = (Activity) context;
            a.startActivityForResult(localIntent, requestCode);
        } else {
            context.startActivity(localIntent);
        }
    }


}
